import 'dart:math';

import 'package:fancy_shimmer_image/fancy_shimmer_image.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';
import 'package:get/get.dart';
import 'package:get/get_core/src/get_main.dart';
import 'package:intl/intl.dart';
import 'package:provider/provider.dart';
import 'package:qianyi/pages/home/community/page_view/like/mock.dart';
import 'package:qianyi/wcao/ui/theme.dart';
import 'package:dart_mock/dart_mock.dart' as mock;
import 'package:shimmer_animation/shimmer_animation.dart';

import '../../../../controller/homeTabController.dart';
import '../../../../controller/imageDetailUserController.dart';
import '../../../../controller/imageDetailVideoController.dart';
import '../../../../entity/home/trendVo.dart';
import '../../../../entity/message/inner/FollowEntity.dart';
import '../../../../entity/mine/User.dart';
import '../../../../network/http_config.dart';
import '../../../../network/http_entity.dart';
import '../../../../network/http_manager.dart';
import '../../../../state/token.dart';
import '../../../../ui/sliverAppBar/animation/animation_enum.dart';
import '../../../../ui/sliverAppBar/sliver_adsorption/sliver_adsorption_appbar.dart';
import '../../../../ui/widget/custom_refresh.dart';
import '../../../../utils/throttle.dart';
import '../../../../view_model/message/inner/follow_view_model.dart';
import '../../../../view_model/mine/trend_view_user_model.dart';
import '../dongtai/PageViewMine.dart';
import '../dongtai/album_view_mine.dart';
import '../dongtai/collection_view_mine.dart';

class MineFirends extends StatefulWidget {
  const MineFirends({Key? key}) : super(key: key);

  @override
  State<MineFirends> createState() => _LikeAndCollectScreenState();
}

class _LikeAndCollectScreenState extends State<MineFirends> {
  HomeTabController homeTabController = Get.find();
  CategoryItem? albumEntity;
  bool isFinish = false;
  TokenController tokenController = Get.find();
  ImageDetailUserController imageDetailUserController = Get.find();
  List<String> images = [
    'https://example.com/image1.jpg', // 替换为实际图片链接
    'https://example.com/image2.jpg', // 替换为实际图片链接
    // 添加更多图片链接...
  ];
  List<String> tabs = ["最新","最热"];
  @override
  void initState() {

    // TODO: implement initState
    super.initState();
    // getAlbumEntity();
  }
  getAlbumEntity() async {
    HudUtil.showBeautiful(context);
    Map<String,String> params1 = {
      "id":homeTabController.tagDetailId!,
    };

    HttpEntity httpEntity1 = await HttpManager.getInstance().request(url: '/platform/tag/getOneTag/',params: params1,method: Method.get) as HttpEntity;

    setState(() {
      albumEntity = CategoryItem.fromJson(httpEntity1.getOriData()['data']);
      this.isFinish = true;
    });
    HudUtil.dismiss();
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
          title: Text("关注记录")

      ),
      body: SafeArea(child: ChangeNotifierProvider(
        create: (context) => FollowViewModel1(),
        child: Consumer<FollowViewModel1>(
            builder: (context, viewModel, child) {

              return

                CustomRefresh(

                    isMaterialHeader:true,
                    controller: viewModel.refreshController,
                    enablePullUp: true,
                    enablePullDown: true,
                    onRefresh: () async {
                      viewModel.onRefresh();
                    },
                    onLoading: () async {
                      viewModel.onLoading();
                    },
                    child: ListView.builder(
                      itemCount: viewModel.datas.length,
                      itemBuilder: (BuildContext context, int index) {
                        FollowItem item = viewModel.datas[index];
                        return GestureDetector(
                            onTap: () async {
                              HudUtil.showBeautiful(context);
                              if(imageDetailUserController.user.length > 0) {
                                imageDetailUserController.user.clear();

                              }

                              Map<String,dynamic> param = {
                                "uid": item.uid
                              };
                              HttpEntity httpEntity = await HttpManager.getInstance().request(url: '/platform/user/getUserInfo',method: Method.get,params: param) as HttpEntity;
                              User user = User.fromJson(httpEntity.getOriData()['data']);
                              imageDetailUserController.user.add(user);
                              imageDetailUserController.userId = item.uid!;
                              HudUtil.dismiss();
                              Get.toNamed("/user/otherUserScreen");
                            },
                            child: Card(

                              margin: EdgeInsets.all(5.0),

                              child: Container(
                                height: 60,
                                child: Row(
                                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                  children: [

                                    Row(
                                      children: [
                                        ClipOval(
                                            child: FancyShimmerImage(imageUrl: '${item.avatar}',width: 40,height: 40,)
                                        )
                                        ,

                                        Center(

                                          child: Column(
                                            mainAxisAlignment: MainAxisAlignment.center,
                                            crossAxisAlignment: CrossAxisAlignment.center,
                                            children: [
                                              SizedBox(
                                                height: 20,
                                                child: Text('${item.username}',
                                                  style: TextStyle(
                                                      fontWeight: FontWeight.w500
                                                  ),
                                                ),

                                              )
                                              ,

                                              Row(
                                                children: [
                                                  Text("${DateFormat.Md().format(
                                                      DateTime.fromMillisecondsSinceEpoch(item.time!)
                                                  )}-"),
                                                  Text("${DateFormat.Hms().format(
                                                      DateTime.fromMillisecondsSinceEpoch(item.time!)
                                                  )}")
                                                ],
                                              )

                                            ],
                                          ),
                                        )
                                      ],
                                    ),
                                    MaterialButton(onPressed: (){
                                      HudUtil.showBeautiful(context);
                                      Map<String,String> params = {
                                        "uid": tokenController.uid,
                                        "fid": item.uid!
                                      };
                                      HttpManager.getInstance().request(url: '/platform/follow/clearFollow',method: Method.post,params: params).then((value) => {
                                        viewModel.onRefresh(),
                                        HudUtil.dismiss()
                                      });
                                    },
                                      child: Text("取关", style: TextStyle(
                                        fontWeight: FontWeight.w300
                                      ),),
                                    )

                                  ],
                                ),
                              ),
                            )
                        )
                        ;
                      },
                    )
                );
            }),
      )),
    );
    ;
  }


}
class CategoryItem {
  String id;
  String? creator;
  String? createDate;
  String count;
  String name;
  String? sort;

  CategoryItem({
    required this.id,
    this.creator,
    this.createDate,
    required this.count,
    required this.name,
    this.sort,
  });

  factory CategoryItem.fromJson(Map<String, dynamic> json) {
    return CategoryItem(
      id: json['id'],
      creator: json['creator'],
      createDate: json['createDate'],
      count: json['count'],
      name: json['name'],
      sort: json['sort'],
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'creator': creator,
      'createDate': createDate,
      'count': count,
      'name': name,
      'sort': sort,
    };
  }
}
